package com.tskj.mh.tabletennis.Interceptor;

import com.tskj.mh.tabletennis.Interceptor.entity.FilterInterfaceList;
import com.tskj.mh.tabletennis.dao.redis.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @notes:
 * @program: easystar2018_ss_v2.0
 * @author: JRX
 * @create: 2019-08-13 15:55
 * 调用接口检测
 **/
@Configuration
public class CheckInterfaceInterceptor implements HandlerInterceptor {

    @Autowired
    private FilterInterfaceList filterInterfaceList;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        String requestURI = request.getRequestURI();
        System.err.println("路径:" + requestURI);
        //过滤字段、路径。。。
        if (!filterInterfaceList.notFilter(requestURI)) {
            System.err.println("跳过的地址:" + requestURI);
        } else {
            try {
                //默认获取url后传递的token
                String token = request.getParameter("token");
//                System.err.println("token是否存在:" + redisUtil.hasKey(token));

                //判断token存在的同时验证失效时间是否即将到期，如果即将到期延长token时效

                //判断token是否存在在redis中
                if (null == token || token.isEmpty()) {
                    response.setStatus(401);
//                    logUtils.emptySErrorLog(request, logModuleConsts.YCGL, "非法访问", "session为空", null, null);
                    return false;
                } else {
                    Object hget = redisUtil.get(token);
                    if (hget == null || hget.toString().isEmpty()) {
                        response.setStatus(401);
//                        logUtils.emptySErrorLog(request, logModuleConsts.YCGL, "非法访问", "session为空", null, null);
                        return false;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                System.err.println("401报错出现的:" + e.getMessage());
//                logUtils.emptySErrorLog(request, logModuleConsts.YCGL, "非法访问", e.getMessage(), "程序异常", null);
                response.setStatus(401);
                return false;
            }
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}